<?php include '../template/template_main.tpl'; 
require("../connect/connect.php");
require ("../admin/insertProb.php");

if(isset($_SESSION["profile"])){
	switch($_SESSION["profile"]){
		case 2: break;
		default: header("Location: index.php");
	}
}
else
header("Location: comoFazerLogin.php");

if(isset($_POST["submit"])){
	$error = array();
	
	$dataInicio = trim($_POST["dataInicio"]);
	$dataFim = trim($_POST["dataFim"]);
	
	if($_POST["probTitulo"] == "")
		$error[] = "Deve preencher o titulo.";
	if($_POST["prob_text"] == "")
		$error[] = "Deve preencher o problema.";
	if($_POST["resposta"] == "")
		$error[] = "Deve preencher a resposta.";
	if($_POST["sugestoes"] == "")
		$error[] = "Deve preencher as sugestões.";
	if (count($error) == 0)
		{
			if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $dataInicio, $parts))
			{
				if(!checkdate($parts[2],$parts[1],$parts[3]))
					$error[] = "Data Inicio: Data está incorrecta.";
			}
			else
				$error[] = "Data Inicio: Formato da data 'DD-MM-YYYY'";
			
			if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $dataFim, $parts))
			{
				if(!checkdate($parts[2],$parts[1],$parts[3]))
					$error[] = "Data Final: Data está incorrecta.";
			}
			else
				$error[] = "Data Final: Formato da data 'DD-MM-YYYY'";
			
			if (count($error) == 0)
			{
				$dtDataInicio = new DateTime($dataInicio." 00:00:00");
				$dtDataFim = new DateTime($dataFim." 00:00:00");
				
				if ($dtDataInicio >= $dtDataFim)
					$error[] = "A data de inicio deve ser superior à data final.";
				else 
				{
					$dataInicio = $dtDataInicio->format("Y-m-d");
					$dataFim = $dtDataFim->format("Y-m-d");
					$error = insertProb($conn, $dataInicio, $dataFim);
					
					if (count($error) == 0)
					{
						$mensagem = "Epoca adicionada com sucesso";
						$_POST["nomeEpoca"] = "";
						$_POST["dataInicio"] = "";
						$_POST["dataFim"] = "";
					}
				}
			}
		}
	
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="../css/template.css" />
<link rel="stylesheet" type="text/css" href="../css/corpo_main.css" />
<script type="text/javascript" src="../js/jquery-1.7.2.js" ></script>
<script type="text/javascript" src="../js/main.js" ></script>

<title>.:MateMatrix:.</title>

</head>
<body>
	<?php makeHeader(); ?>
	<?php echo $estruturaInicio;?>
	<div id="title">
		<h1>- Criar Problema -</h1>
	</div>
	<div id="problemaDiv">
			<?php
			if (isset($error) && (count($error) > 0))
			{
				echo "<div class=\"erros\"><ul>";
				foreach ($error as $value) {
					printf("<li>%s</li>", $value);
				}
				echo "</ul></div>";
			}
			if (isset($mensagem))
			{
				echo "<div class=\"info\"><ul><li>$mensagem</li></ul></div>";
			}
		?>
		<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
			<div>
				Título:<br />
			</div>
			<div>
				<input type="text" name="probTitulo" value="<?php if (isset($_POST["probTitulo"])) echo htmlspecialchars($_POST["probTitulo"]); ?>"/>
			</div>
			<div>
				Problema:<br />
				<textarea name="prob_text" rows="10" cols="100"><?php if (isset($_POST["prob_text"])) echo htmlspecialchars($_POST["prob_text"]); ?></textarea>
			</div>
			<div>
				Resposta:<br />
				<textarea name="resposta" rows="10" cols="100"><?php if (isset($_POST["resposta"])) echo htmlspecialchars($_POST["resposta"]); ?></textarea>
			</div>
			<div>
				Sugestões:<br />
				<textarea name="sugestoes" rows="10" cols="100"><?php if (isset($_POST["sugestoes"])) echo htmlspecialchars($_POST["sugestoes"]); ?></textarea>
			</div>
			<div> 
				Data Início: &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
				Data Fim:<br />
				<input type="text" name="dataInicio" value="<?php if (isset($_POST["dataInicio"])) echo htmlspecialchars($dataInicio); ?>" />
				<input type="text" name="dataFim" value="<?php if (isset($_POST["dataFim"])) echo htmlspecialchars($dataFim); ?>" />				
			</div>
			<div>
				<input type="submit" value="Guardar" name="submit" />
				<input type="reset" value="Limpar"  />
			</div>
		</form>
	</div>
	<?php echo $estruturaFim; ?>
	<?php echo $footer;?>
</body>
</html>